/* Script.h */
/* author: Jiqing Wu (jiqingwu@gmail.com) */
/* create: 2012-01-03 */
/* update: 2014-05-01 Thu */

#ifndef SCRIPT_H_
#define SCRIPT_H_

#include "TextParser.h"

#define VAR_NUMBER 50

using namespace std;

void RunScripts( char * szName );
void RunScripts();

struct stVariable
{
	char szName[32];
	short nValue;
};

/* a script line struct */
struct stScriptLine
{	
	char szScriptLine[ 256 ];
};

/* a table entry, speed finding script */
struct stScriptIndex
{
	short nIndex;
	char szScriptName[32];
};


class CScript
{
private:
	CTextParser	*pParsedFile;		// handle of parsered file
	short		nScriptNumber;		//record number
	short		nScriptLineNumber;	//script lines' number
	short		nIndexInLine;
	short		nCurrentLine;
	/* char		cRecordHead;		//record head char */
	stScriptLine	*pScripts;
	stScriptIndex	*pTable;
    stVariable vars[VAR_NUMBER];

public:
	CScript(const char * szFileName, const char cHeadFlag);
	~CScript();
    void DebugInfo();
	void RunScripts( const char * szName );		// run scripts	
	bool FindScripts( const char * szName );	//find scripts whose name is given
	void ExecuteScriptLine( );

    //clear variables
    void ClearVariables();
    void ClearVariable(const char *szName);
    // get variable's value
    short GetVariableValue( const char * szName );
    //set variable's value
    bool SetVariableValue( const char * szName, short nValue);
    // whether variable is exist
    bool IsVariableExist( const char * szName );
    // add variable
    bool AddVariable( const char * szName, short nValue = 0);

private:
	bool Expression(short *);	//求表达式的值
	void LoadScripts( );	// load scripts	
};

#endif
